package com.yanqu.road.dao.impl.activity.douluodalu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuBox;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class DouLuoDaLuBoxDaoImpl extends TempDataObjectDao<DouLuoDaLuBox> {

    public static String getDouLuoDaLuBoxSql = "select * from `t_c_activity_douluodalu_box` where `activityId`=? and groupId=?";
    public DouLuoDaLuBox getDouLuoDaLuBox(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuBoxSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertDouLuoDaLuBoxSql = "insert into `t_c_activity_douluodalu_box`(activityId,count,groupId) values (?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuBox data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCount()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuBoxSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuBoxSql = "update `t_c_activity_douluodalu_box` set count=? where activityId=? and groupId=?";
    @Override
    protected boolean updateData(DouLuoDaLuBox data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getCount()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuBoxSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuBox parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuBox data = new DouLuoDaLuBox();
        data.setActivityId(rs.getInt("activityId"));
        data.setCount(rs.getInt("count"));
        data.setGroupId(rs.getInt("groupId"));

        return data;
    }
}
